<?php

/**
* @copyright   (C) 2010 iJoomla, Inc. - All rights reserved.
* @license  GNU General Public License, version 2 (http://www.gnu.org/licenses/gpl-2.0.html) 
* @author  iJoomla.com webmaster@ijoomla.com
* @url   http://www.ijoomla.com/licensing/
* the PHP code portions are distributed under the GPL license. If not otherwise stated, all images, manuals, cascading style sheets, and included JavaScript  
* are NOT GPL, and are released under the IJOOMLA Proprietary Use License v1.0 
* More info at http://www.ijoomla.com/licensing/
*/

defined('_JEXEC') or die('Restricted access');

class QuestionsList{
	
	function getQuestionsList($survey_id, $current_page_id, $params, $completed, $session_id){
		$all_questions = $this->getAllQuestions($survey_id, $current_page_id);
		$preview = JRequest::getVar("previews_page_id", "");		
		$return = '<table width="100%">';
		$validation_form = 'function validate_form(thisform) {'."\n";
		
		if(count($all_questions)>0){
			foreach($all_questions as $key=>$question){
				$question_details = $this->getQuestionDetails($question["id"]);
				$id = $question_details["0"]["id"];
				$orientation = $question_details["0"]["orientation"];
				$question_type = $question_details["0"]["question_type"];				
				$question = new Question($id, $survey_id, $orientation, $question_type, $params, $question_details, $completed, $session_id);				
				$result = $question->display();
				$validation_form .= $result["validation"]."\n";
				
				$return .= '<tr>';
				$return .= 	   '<td>';
				$return .= 	   	$result["content"];	
				$return .= 	   '</td>';
				$return .= '</tr>';
			}
		}
		
		$return .= 		"<tr width=\"100%\">";
		$return .= 		 	"<td>";
		$return .= 		 		"<table width=\"100%\">";
		$return .= 					"<tr width=\"100%\">";		
		
		$pages = $this->getAllPages($survey_id);	
		
		if(isset($pages) && isset($pages["0"]) && is_array($pages) && $current_page_id != $pages["0"]["id"] && $params->general_value != "ORpRFO"){	
			$return .= 			"<td align=\"right\" width=\"50%\">";
			$return .= 			 '<input type="submit" onclick="document.survey_content.task.value=\'back\'" class="'.$params->button.'" value="&lt;&lt; '.JText::_("COM_SURVEYS_BACK").'" name="Back">';
			$return .= 			"</td>";
		}
		else{
			$return .= 			"<td align=\"right\" width=\"50%\">";			
			$return .= 			"</td>";
		}
		$return .= 			"<td align=\"left\" width=\"50%\">";
		
		// next botton for preview in admin
		$controller = JRequest::getVar("controller", "", "get");
		if(isset($pages) && isset($pages["0"])){
			if($controller != "" && $controller == "prevsurvey"){
				$return .= '<input type="submit" onclick="return validate_form(form);" class="'.$params->button.'" value="'.JText::_("COM_SURVEYS_NEXT").' &gt;&gt;" name="submit">';
			}
			else{			
				$return .= '<input type="submit" onclick="return validate_form(form)" class="'.$params->button.'" value="'.JText::_("COM_SURVEYS_NEXT").' &gt;&gt;" name="submit">';
			}
		}	
		$return .= 			"</td>";
		$return .= 					"</tr>";
		$return .= 		 		"</table>";
		$return .= 		 	"</td>";
		$return .= 		 "</tr>";		
		$return .= '</table>';	
		
		if($controller != "" && $controller == "prevsurvey"){
			$validation_form .= '	document.survey_content.task.value="next";'."\n";
		}
		else{	
			$validation_form .= '	document.survey_content.task.value="save_survey";'."\n";
		}	
		$validation_form .= "}";
		$document =& JFactory::getDocument();
		$document->addScriptDeclaration($validation_form);	
		return $return;
	}
	
	function getAllPages($survey_id){
		$db =& JFactory::getDBO();		
		$query = $db->getQuery(true);
		$query->clear();		
		$query->select('id');
		$query->from('#__ijoomla_surveys_pages');
		$query->where("survey_id=".$survey_id." and published=1");
		$query->order("ordering asc");
		$db->setQuery($query);		
		$db->query();
		$result = $db->loadAssocList();		
		return $result;
	}
	
	function getAllQuestions($survey_id, $current_page_id){
		$data = new JDate();
		$db =& JFactory::getDBO();		
		$query = $db->getQuery(true);
		$query->clear();
		$query->select('id, question_type');
		$query->from('#__ijoomla_surveys_questions');
		$query->where("survey_id=".$survey_id." and page_id=".$current_page_id." and published=1 and published_up <= '".$data."' and ((published_down >= '".$data."') or published_down='0000-00-00 00:00:00')");
		$query->order("ordering asc");
		$db->setQuery($query);		
		$db->query();
		$result = $db->loadAssocList();
		return $result;
	}
	
	function getQuestionDetails($id){
		$db =& JFactory::getDBO();		
		$query = $db->getQuery(true);
		$query->clear();
		$query->select('*');
		$query->from('#__ijoomla_surveys_questions');
		$query->where("id=".$id);
		$db->setQuery($query);		
		$db->query();
		$result = $db->loadAssocList();		
		return $result;	
	}	
	
};

class Question{
	var $_orientation = "";
	var $_question_id = "";
	var $_survey_id = "";
	var $_class = "";
	var $_params = "";
	var $_question_details = "";
	var $_completed = "";
	var $_session_id = "";
	
	function Question($quest_id="0", $survey_id="0", $orien="", $type="", $params="", $question_details="", $completed="", $session_id="0"){
		$this->_orientation = $orien;
		$this->_question_id = $quest_id;
		$this->_survey_id = $survey_id;
		$this->_class = $this->getClass($type);	
		$this->_params = $params;
		$this->_question_details = $question_details;
		$this->_completed = $completed;
		$this->_session_id = intval($session_id);
	}
	
	function display(){
		include_once(JPATH_ROOT.DS."components".DS."com_surveys".DS."helpers".DS."QuestionEssay.php");
		include_once(JPATH_ROOT.DS."components".DS."com_surveys".DS."helpers".DS."QuestionMultiplePerRow.php");
		include_once(JPATH_ROOT.DS."components".DS."com_surveys".DS."helpers".DS."QuestionOneAnswer.php");
		include_once(JPATH_ROOT.DS."components".DS."com_surveys".DS."helpers".DS."QuestionOneAnswerMenu.php");
		include_once(JPATH_ROOT.DS."components".DS."com_surveys".DS."helpers".DS."QuestionMultipleAnswer.php");
		include_once(JPATH_ROOT.DS."components".DS."com_surveys".DS."helpers".DS."QuestionOnePerRow.php");
		include_once(JPATH_ROOT.DS."components".DS."com_surveys".DS."helpers".DS."QuestionEndedOneLine.php");
		include_once(JPATH_ROOT.DS."components".DS."com_surveys".DS."helpers".DS."QuestionEndedMoreLines.php");
		include_once(JPATH_ROOT.DS."components".DS."com_surveys".DS."helpers".DS."QuestionConstant.php");
		include_once(JPATH_ROOT.DS."components".DS."com_surveys".DS."helpers".DS."QuestionDateTime.php");
		include_once(JPATH_ROOT.DS."components".DS."com_surveys".DS."helpers".DS."QuestionDateTime.php");
		include_once(JPATH_ROOT.DS."components".DS."com_surveys".DS."helpers".DS."QuestionMultiplePerRowMenus.php");
		
		$total_class = "Question".$this->_class;
		$question = new $total_class();		
		$result = $question->display($this->_orientation, $this->_question_id, $this->_survey_id, $this->_params, $this->_question_details, $this->_completed, $this->_session_id);
		return $result;
	}
	
	function getClass($type){
		$class = "";
		switch($type){
			case "1" : {
				$class = "OneAnswer";
				break;
			}
			case "2" : {
				$class = "OneAnswer";
				break;
			}
			case "3" : {
				$class = "OneAnswerMenu";
				break;
			}
			case "4" : {
				$class = "MultipleAnswer";
				break;
			}
			case "5" : {
				$class = "MultipleAnswer";
				break;
			}
			case "6" : {
				$class = "OnePerRow";
				break;
			}
			case "7" : {
				$class = "MultiplePerRow";
				break;
			}
			case "8" : {
				$class = "MultiplePerRowMenus";				
				break;
			}
			case "9" : {
				$class = "EndedOneLine";
				break;
			}
			case "10" : {
				$class = "EndedMoreLines";
				break;
			}
			case "11" : {
				$class = "Essay";
				break;
			}
			case "12" : {
				$class = "Constant";
				break;
			}
			case "13" : {
				$class = "DateTime";
				break;
			}
		}
		return $class;
	}
	
};

?>